<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="我的昵称" prop="nickname">
        <el-input v-model="formData.nickname" />
      </el-form-item>
      <el-form-item label="真实姓名" prop="stuName">
        <el-input v-model="formData.stuName" />
      </el-form-item>
      <el-form-item label="我的性别" prop="sex">
        <el-radio-group v-model="formData.sex">
          <el-radio
            v-for="dict in getIntDictOptions(DICT_TYPE.SYSTEM_USER_SEX)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="我的简介" prop="stuDesc">
        <el-input v-model="formData.stuDesc" type="textarea" />
      </el-form-item>
      <el-form-item label="居住地" prop="stuAddr">
        <el-input v-model="formData.stuAddr" />
      </el-form-item>
      <el-form-item label="毕业年份" prop="graduationYear">
        <el-select
          v-model="formData.graduationYear"
          placeholder="请选择毕业年份"
          :disabled="isSalaryMeet"
        >
          <el-option v-for="year in graduationYears" :key="year" :label="year" :value="year" />
        </el-select>
      </el-form-item>
      <el-form-item label="学历" prop="stuEdu">
        <el-select v-model="formData.stuEdu">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.STU_EDU_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="求职意向" prop="jobTitleId">
        <el-select
          v-model="formData.jobTitleId"
          multiple
          filterable
          placeholder="请选择意向岗位"
        >
          <el-option
            v-for="item in jobTitleList"
            :key="item.jobTitleId"
            :label="item.jobTitle"
            :value="item.jobTitle"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="毕业院校" prop="universityFrom">
        <el-input v-model="formData.universityFrom" :disabled="isBuild" />
      </el-form-item>
      <el-form-item label="专业" prop="majorName">
        <el-input v-model="formData.majorName" :disabled="isBuild" />
      </el-form-item>
      <el-form-item label="" prop="salaryMeet">
        <el-checkbox v-model="isBuild" @change="handleIsBuild">共建院校</el-checkbox>
      </el-form-item>
      <el-form-item label="共建院校" prop="universityId">
        <!-- 一级下拉框 -->
        <el-select
          v-model="formData.universityId"
          :disabled="!isBuild"
          placeholder="请选择共建院校"
          @change="handleBuildSchool($event)"
        >
          <el-option
            v-for="item in buildSchool"
            :key="item.schId"
            :label="item.schName"
            :value="item.schId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="共建专业" prop="majorId">
        <!-- 二级下拉框 -->
        <el-select v-model="formData.majorId" :disabled="!isBuild" placeholder="请选择专业">
          <el-option
            v-for="item in buildMajor"
            :key="item.majId"
            :label="item.majName"
            :value="item.majId"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">发布</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { StuMstApi, StuMstVO } from '@/api/dbit/stumst'
import { SchInfoApi, SchInfoVO } from '@/api/dbit/schinfo'
import { JobTitlesApi } from '@/api/dbit/jobtitles'
import { build } from 'pdfjs-dist'

/** 企业职位信息 表单 */
defineOptions({ name: 'StuMstInfoForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
let isSalaryMeet = ref(false) // 是否薪资面议

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const isBuild = ref(false) // 是否是共建院校
const buildSchool = ref([]) // 共建院校列表
const buildMajor = ref([]) // 共建专业列表
const jobTitleList = ref([]) // 职位列表

let formData = reactive({
  stuId: undefined,
  stuName: undefined,
  stuEdu: undefined,
  universityId: undefined,
  universityFrom: undefined,
  majorId: undefined,
  majorName: undefined,
  graduationYear: undefined,
  stuAddr: undefined,
  stuDesc: undefined,
  nickname: undefined,
  avatar: undefined,
  sex: undefined,
  jobTitleId: undefined
})

// 验证规则
const formRules = reactive({
  universityId: [{ required: true, message: '请选择共建院校', trigger: 'blur' }],
  majorId: [{ required: true, message: '请选择共建专业', trigger: 'blur' }]
})

const graduationYears = ref([]) // 毕业年份数组
const currentYear = new Date().getFullYear()
// 初始化年份数组
for (let year = 1980; year <= currentYear + 5; year++) {
  graduationYears.value.push(year)
}
// 切换是否是共建
const handleIsBuild = () => {
  if (isBuild.value) {
    formData.universityFrom = undefined
    formData.majorName = undefined
  } else {
    formData.universityId = undefined
    formData.majorId = undefined
  }
}
// 切换共建院校下拉选项
const handleBuildSchool = async (event: number) => {
  const data = await SchInfoApi.getMajorList(event)
  buildMajor.value = data
  formData.majorId = undefined
}

const formRef = ref() // 表单 Ref

/** 获取共建院校列表 */
const SchInfoList = async () => {
  const data = await SchInfoApi.getSchInfoList()
  buildSchool.value = data
}

/** 获取求职岗位列表 */
const JobTitleList = async () => {
  const data = await JobTitlesApi.getJobTitlesList()
  jobTitleList.value = data
}

/** 打开弹窗 */
const open = async (type: string, stuMstInfo) => {
  // resetForm()
  Object.assign(formData, stuMstInfo)
  SchInfoList()
  JobTitleList()
  dialogVisible.value = true
  dialogTitle.value = t(type)
  formType.value = type
  // 根据用户传递过来的信息,判断是否是共建院校
  isBuild.value =
    stuMstInfo.universityId === undefined || stuMstInfo.universityId === null ? false : true
  if (isBuild.value) {
    // 是共建
    const data = await SchInfoApi.getMajorList(formData.universityId)
    buildMajor.value = data
  }
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await JobInfoApi.getJobInfo(id)
      isSalaryMeet.value = formData.value.salaryMeet === 1 ? true : false
      /** 将checkBox选中 */
      salaryMeetCheck.value = formData.value.salaryMeet === 1 ? true : false
      if (salaryMeetCheck.value) {
        handleSalaryMeetChange(true)
      }
      const arr = await JobInfoApi.showCascader(formData.value.jobCity)
      formData.value.jobCity = arr
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  if (isBuild.value) {
    await formRef.value.validate()
  }
  // 提交请求
  formLoading.value = true
  try {
    if (formData.jobTitleId != undefined) {
      formData.jobTitleId = formData.jobTitleId.join(';')
    }
    const data = formData as unknown as StuMstVO
    await StuMstApi.updateStuMstInfo(data)
    message.success(t('common.updateSuccess'))
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData = {
    stuId: undefined,
    stuName: undefined,
    stuEdu: undefined,
    universityId: undefined,
    universityFrom: undefined,
    majorId: undefined,
    majorName: undefined,
    graduationYear: undefined,
    stuAddr: undefined,
    stuDesc: undefined,
    nickname: undefined,
    avatar: undefined,
    sex: undefined
  }
  buildMajor.value = []
  formRef.value?.resetFields()
}
</script>
